package com.xiaoying.server.publish.task;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.Resource;

import org.apache.commons.collections.FastArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.omg.CORBA.CTX_RESTRICT_SCOPE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;


import com.xiaoying.server.publish.entity.FileInfo;
import com.xiaoying.server.publish.entity.UploadResult;
import com.xiaoying.server.publish.service.UploadResultService;
import com.xiaoying.server.publish.utils.RedisUtil;
import com.xiaoying.server.publish.utils.SpringContext;

public class ResultDealTask implements Runnable{	
	private static Log log = LogFactory.getFactory().getInstance(ResultDealTask.class);
	
	private UploadResultService uploadResultService;	
	private BlockingQueue<UploadResult> uploadResultQueue;
	private UploadResult uploadResult;
	private AtomicInteger idleThread;
	private BlockingQueue<String> redisBackupQueue;
	private String key;
	
	public ResultDealTask(BlockingQueue<UploadResult> resQueue,UploadResult uploadResult,
						UploadResultService uploadResultService,AtomicInteger idleThread){
		this.uploadResultQueue = resQueue;
		this.uploadResult = uploadResult;
		this.uploadResultService = uploadResultService;
		this.idleThread = idleThread;
	}
	
	public ResultDealTask(BlockingQueue<UploadResult> resQueue,UploadResult uploadResult,
			UploadResultService uploadResultService,AtomicInteger idleThread,BlockingQueue<String> redisBackupQueue){
		this.uploadResultQueue = resQueue;
		this.uploadResult = uploadResult;
		this.uploadResultService = uploadResultService;
		this.idleThread = idleThread;
		this.redisBackupQueue = redisBackupQueue;
		this.key = RedisUtil.PREFIX_XY_BACKUP_UPLOAD_FILEINFO+
						uploadResult.getFileName()+"_"+uploadResult.getReceiveDt();
	}
	
	@Override
	public void run() {
		// 设置线程工作状态
		this.setThreadStatusBusy();
		this.saveResult(uploadResult);
		// 释放线程
		this.releaseThread();
	}
	
	private void saveResult(UploadResult uploadResult){		
		if(uploadResultService.save(uploadResult)!=null){
			this.redisBackupQueue.add(key);
//			log.info("文件 ["+uploadResult.getFileName()+"] 上传结果入库成功!");
//			System.out.println("文件 ["+uploadResult.getFileName()+"] 处理结果入库成功!");
		}else{
			// 入库失败放入结果队列上传
			this.uploadResultQueue.add(uploadResult);
			log.error("文件 ["+uploadResult.getFileName()+"] 处理结果入库失败!");
		}		
	}

	/**
	 * 设置线程状态繁忙
	 * @return
	 */
	private int setThreadStatusBusy(){
		return this.idleThread.decrementAndGet();
	}
	
	/**
	 * 设置释放线程
	 * @return
	 */
	private int releaseThread(){
		return this.idleThread.incrementAndGet();
	}
	

	public BlockingQueue<UploadResult> getUploadResultQueue() {
		return uploadResultQueue;
	}

	public void setUploadResultQueue(BlockingQueue<UploadResult> uploadResultQueue) {
		this.uploadResultQueue = uploadResultQueue;
	}

	public UploadResult getUploadResult() {
		return uploadResult;
	}

	public void setUploadResult(UploadResult uploadResult) {
		this.uploadResult = uploadResult;
	}
	
}
